<?php
namespace app\api\model;


use think\Model;
use think\Session;

/**
 * Class Manager
 * @package app\api\model
 */
class Manager extends Model
{
    /**
     * 增加管理员
     * @return false|int
     */
    public function addmanager(){
        $res=$this->save([
            "nickname"=>request()->param("nickname"),
            "username"=>request()->param("username"),
            "password"=>md5(request()->param("password")),
            "authlist"=>array_key_exists("auth",request()->post())?implode(",",request()->post()["auth"]):"",
            "merchantid"=>request()->param("merchantid"),
            "type"=>1
        ]);
        return $res;
    }

    /**
     * 修改管理员
     */
    public function editmanager(){
        $data['username']=request()->param("username");
        if(request()->post("password")){
            $data['password']=md5(request()->param("password"));
        }
        $data['authlist']=array_key_exists("auth",request()->post())?implode(",",request()->post()["auth"]):"";
        $res=$this->save($data,["id"=>request()->param("managerid")]);
        return $res;
    }

    /**
     * 获取管理员列表
     */
    public function getall($merchantid){
        return $this->where(["merchantid"=>$merchantid,"type"=>1])->select();
    }

    /**
     * 获取管理员一个
     */
    public function getone($managerid){
        return $this->where(["id"=>$managerid,"type"=>1])->find();
    }

    /**
     * 增加收银员
     * @return false|int
     */
    public function addcashier(){
        $res=$this->save([
            "nickname"=>request()->param("nickname"),
            "username"=>request()->param("username"),
            "password"=>md5(request()->param("password")),
            "authlist"=>array_key_exists("auth",request()->post())?implode(",",request()->post()["auth"]):"",
            "shopid"=>request()->param("shopid"),
            "type"=>2
        ]);
        return $res;
    }

    /**
     * 修改收银员
     */
    public function editcashier(){
        $data['username']=request()->param("username");
        if(request()->post("password")){
            $data['password']=md5(request()->param("password"));
        }
        $data['authlist']=array_key_exists("auth",request()->post())?implode(",",request()->post()["auth"]):"";
        $res=$this->save($data,["id"=>request()->param("managerid")]);
        return $res;
    }

    /**
     * 获取收银员列表
     */
    public function getallcashier($shopid){
        return $this->where(["shopid"=>$shopid,"type"=>2])->select();
    }

    /**
     * 获取收银员一个
     */
    public function getonecashier($managerid){
        return $this->where(["id"=>$managerid,"type"=>2])->find();
    }

    /**
     * 检查登录
     */
    public function checklogin(){
        $res=$this->where(["username"=>request()->post("username")])->find();
        if($res){
            if($res["password"]==md5(request()->post("password"))){
                Session::set("managerid",$res["id"]);//设置managerid
                Session::set("usertype",$res["type"]);
                if($res["type"]==1){
                    Session::set("merchantid",$res["merchantid"]);
                }else{
                    Session::set("shopid",$res["shopid"]);
                    Session::set("shopname",\model("shop")->getname($res["shopid"]));
                }
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
    }

    /**
     * APP登录验证
     * @return  ["code"=>1,"data"=>"","msg"=>""]
     */
    public function applogin(){
        $res=$this->where(["username"=>request()->post("username")])->find();
        if($res){
            sendsocket(json_encode(array('status'=>1,'tipstype'=>4,'msg'=>"您的账号已在其它终端登录,可能存在安全风险,请重新登录!")),$res['id']);//新增消息
            if($res["password"]==md5(request()->post("password"))){
                //生成TOKEN
                $token=md5(time().$res["id"]);
                $up=$this->isUpdate(true)->save([
                    "token"=>$token
                ],["id"=>$res["id"]]);
                if($up){
                    return ["code"=>1,"msg"=>"登录成功","data"=>[
                        "usertype"=>$res["type"],
                        "shopid"=>$res["shopid"],
                        "authlist"=>$res["authlist"],
                        "token"=>$token
                    ]];
                }else{
                    return ["code"=>0,"data"=>"","msg"=>"请重试!"];
                }
            }else{
                return ["code"=>0,"data"=>"","msg"=>"密码错误"];
            }
        }else{
            return ["code"=>0,"data"=>"","msg"=>"用户不存在"];
        }
    }

    /**
     * 小天APP专用
     * @return array
     */
    public function codelogin(){
        $res=$this->where(["username"=>request()->post("username")])->find();
        if($res){
            sendsocket(json_encode(array('status'=>1,'tipstype'=>4,'msg'=>"您的账号已在其它终端登录,可能存在安全风险,请重新登录!")),$res['id']);//新增消息
            //生成TOKEN
            $token=md5(time().$res["id"]);
            $up=$this->isUpdate(true)->save([
                "token"=>$token
            ],["id"=>$res["id"]]);
            if($up){
                return ["code"=>1,"msg"=>"登录成功","data"=>[
                    "usertype"=>$res["type"],
                    "authlist"=>$res["authlist"],
                    "shopid"=>$res["shopid"],
                    "token"=>$token
                ]];
            }else{
                return ["code"=>0,"data"=>"","msg"=>"请重试!"];
            }
        }else{
            return ["code"=>0,"data"=>"","msg"=>"用户不存在"];
        }
    }

    /**
     * POS验证码登录
     * @return array
     */
    public function poscodelogin(){
        $res=$this->where(["username"=>request()->post("username")])->find();
        if($res){
            sendsocket(json_encode(array('status'=>1,'tipstype'=>4,'msg'=>"您的账号已在其它终端登录,可能存在安全风险,请重新登录!")),$res['id']);//新增消息
//            if($res["password"]==md5(request()->post("password"))){
                //生成TOKEN
            $token=md5(time().$res["id"]);
            $up=$this->isUpdate(true)->save([
                "token"=>$token
            ],["id"=>$res["id"]]);
            if($up){
                return ["code"=>1,"msg"=>"登录成功","data"=>[
                    "usertype"=>$res["type"],
                    "shopid"=>$res["shopid"],
                    "authlist"=>$res["authlist"],
                    "token"=>$token
                ]];
            }else{
                return ["code"=>0,"data"=>"","msg"=>"请重试!"];
            }
//            }else{
//                return ["code"=>0,"data"=>"","msg"=>"密码错误"];
//            }
        }else{
            return ["code"=>0,"data"=>"","msg"=>"用户不存在"];
        }
    }

}